package com.example.springbootwl08.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootwl08.common.componet.R;
import com.example.springbootwl08.product.pojo.entity.Product;
import com.example.springbootwl08.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gxl
 * @since 2022-11-09
 */
@Controller
@CrossOrigin
public class ProductController {


    @Autowired
    private IProductService iProductService;

    @RequestMapping("/toProList")
    public String toProList(){
        return "product/product_list";
    }
    @RequestMapping("/toProListVue")
    public String toProListVue(Model model){
//        IPage page = new Page(0,5);
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("product_name","小米");
//        IPage productPage = iProductService.page(page,queryWrapper);
//        model.addAttribute("productPage",productPage);
        return "product/product_list-vue";
    }
    /**
     * @Author GongXL
     * @Description  更新产品
     * @Date 14:51 2022/11/13
     * @Param [sunpinewater.tech.product.entity.Product]
     * @return sunpinewater.tech.common.componet.R
     **/
    @RequestMapping("/editProVue")
    @ResponseBody
    public R editProVue(@RequestBody Product product){
        boolean b = iProductService.updateById(product);
        return R.ok("产品更新成功");
    }
    /**
     * @Author GongXL
     * @Description  分页
     * @Date 9:55 2022/11/10
     * @Param [org.springframework.ui.Model]
     * @return com.baomidou.mybatisplus.core.metadata.IPage
     **/
    @RequestMapping("/toProListByPageVue")
    @ResponseBody
    public IPage toProListByPageVue(@RequestParam(value = "curpage",defaultValue = "1",required = false) Integer curpage,@RequestParam(value = "pagesize",defaultValue = "5",required = false) Integer pagesize){
        IPage page = new Page(curpage,pagesize);
        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("product_name","小米");
        IPage productPage = iProductService.page(page,queryWrapper);
        return productPage;
    }
    /**
     * 查看或回显商品信息，get：查看商品信息  update:为修改页回显的商品信息
     */
    @ResponseBody
    @RequestMapping("/product/{productId}")
    public Product getProduct(@PathVariable("productId") String productId) {
        Product product = iProductService.getById(productId);
        return product;
    }
    /**
     * 新增商品
     *
     * @param product
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    public Product addProduct(@RequestBody Product product) {
        iProductService.save(product);
        return product;
    }
    /**
     * 删除指定的商品
     *
     * @param productId
     * @return
     */
    @RequestMapping(value = "/product", method = RequestMethod.DELETE)
    public String deleteProduct(String productId) {
        iProductService.removeById(productId);
        return "redirect:/toProList";
    }
    /**
     * 获取商品列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getProductList1")
    public List<Product> getProductList1() {
        List productList = iProductService.list();
        return productList;
    }
    /**
     * 修改商品信息
     *
     * @param product
     * @return
     */
    @RequestMapping(value = "/edit-product")
    public String updateProduct(Product product) {
        iProductService.updateById(product);
        return "redirect:/toProList";
    }

}
